package com.tender.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.tender.facade.StockFacadeSentinelApi;

/**
 * openFeign 和 sentinel 进行服务降级和流量监控的整合处理demo
 */
@RestController
@RefreshScope // 以 这个bean 为单位，可以动态感知里面的属性的变化
public class StockController implements StockFacadeSentinelApi {

    @Value("${server.port}")
    private String port;

    @Override
    @SentinelResource(value = "deduceStockByOrderNo", blockHandler = "deduceStockByOrderNorBlockHandler")
    public String deduceStockByOrderNo(@PathVariable String orderNo) {
        return "方法：deduceStockByOrderNo " + " 端口：" + port + " 服务正常响应：O(∩_∩)O" + " orderNo = " + orderNo;
    }

    public String deduceStockByOrderNorBlockHandler(String orderNo, BlockException e) {
        return "方法：deduceStockByOrderNo " + " 端口：" + port + " 服务被Sentinel流控 /(ㄒoㄒ)/~~" + " orderNo = " + orderNo + " 异常：" + e.getMessage();
    }

    @Override
    @SentinelResource(value = "checkStockInfoByNo", blockHandler = "checkStockInfoByNoBlockHandler")
    public String checkStockInfoByNo(@PathVariable String stockNo) {
        return "方法：checkStockInfoByNo " + " 端口：" + port + " 服务正常响应：O(∩_∩)O" + " stockNo = " + stockNo;
    }

    public String checkStockInfoByNoBlockHandler(String stockNo, BlockException e) {
        return "方法：checkStockInfoByNo " + " 端口：" + port + " 服务被Sentinel流控 /(ㄒoㄒ)/~~" + " stockNo = " + stockNo + " 异常：" + e.getMessage();
    }
}
